import 'package:flutter/material.dart';

class LoadingUtils{
  static OverlayEntry? _overlayEntry;

  static void showLoading(BuildContext context){
    if(_overlayEntry != null) {
      _cancel();
    }
    _overlayEntry = OverlayEntry(builder: (BuildContext context) => _LoadingWidget());
    Overlay.of(context).insert(_overlayEntry!);
  }

  static void hideLoading(){
    _cancel();
  }

  static void _cancel(){
    _overlayEntry?.remove();
    _overlayEntry = null;
  }


}

class _LoadingWidget extends StatelessWidget {
  const _LoadingWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: (){
          LoadingUtils.hideLoading();
        },
        child: Container(
            width: double.infinity,
            height: double.infinity,
            color: Colors.black38,
            alignment: Alignment.center,
            child: Container(
              width: 130,
              height: 130,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(20)),
                  color: Colors.white
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  CircularProgressIndicator(
                    strokeWidth: 5,
                    valueColor: AlwaysStoppedAnimation(Color(0xFFFF8730)),
                  ),
                  SizedBox(height: 25,),
                  Text("正在加载...",style: TextStyle(color: Colors.black54,fontSize: 16,decoration: TextDecoration.none),)
                ],
              ),

            )
        )
    );
  }
}
